package model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import services.PartService;
import services.StateService;
import services.StepService;

import manager.Start;

public class Board extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1110967850399041590L;

	Image bg;
	private final int boardPosX = 200;
	
	private final int boardPosY = 200;
	
	private int polygonRadius;
	
	private Part[][] parts;
	
	public Part[][] getParts() {
		return parts;
	}
	
	List<Part> allPartList = new LinkedList<Part>();
	
	public List<Part> getAllPartList() {
		return allPartList;
	}
	
	private int boardSize;
	public int getBoardSize() {
		return boardSize;
	}
	
	/** 
	 * constructor
	 */
	public Board() {
		initBoardSize();
		
		initPolygonRadius();
		
		setVisible(true);
		parts = new Part[boardSize][boardSize];
		
		addMouseListener(new MouseHandler());
	}
	
	private void initBoardSize() {
		Object[] options = {" 5 x 5", "7 x 7", "9 x 9"};
		
		boardSize = JOptionPane.showOptionDialog(Start.getGame(), 
												 "Please choose the size of the board", 
												 "Select Board Size", 
												 JOptionPane.YES_NO_CANCEL_OPTION, 
												 JOptionPane.QUESTION_MESSAGE, 
												 null, 
												 options, 
												 options[2]);
		
		switch (boardSize) {
		
		case 0:
			boardSize = 9;
			
			break;
		case 1: 
			boardSize = 13;
			break;
		case 2:
			boardSize = 17;
			break;
		default:
			break;
		}
	}
	
	private void initPolygonRadius() {
		switch (boardSize) {
		case 9:
			polygonRadius = 35;
			break;
		case 13: 
			polygonRadius = 25;
			break;
		case 17:
			polygonRadius = 20;
		default:
			break;
		}
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponents(g);
		handleBackground(g);
		
		for (int i = 0; i < boardSize; i++) {
			for (int j = 0; j < boardSize; j++) {
				if ((j+i)%2==0) {
					int[] polyX = new int[6];
					int[] polyY = new int[6];
					
					for (int k = 0; k < 6; k++) {
						polyX[k] = (int)(polygonRadius*Math.cos(k*Math.PI/6 * 2) + boardPosX + j*1.65*polygonRadius);
						polyY[k] = (int)(polygonRadius*Math.sin(k*Math.PI/6 * 2) + boardPosY + i*polygonRadius);
						
					}
					
					if (parts[i][j] == null) {
						parts[i][j] = new Part(polyX, polyY, i, j, Color.CYAN);
						allPartList.add(parts[i][j]);						
					}
					
					g.setColor(parts[i][j].getColor());
					
					g.fillPolygon(polyX, polyY, 6);
					
					g.setColor(Color.black);
					
					g.drawPolygon(polyX, polyY, 6);
				}
			}
			
		}
	}
	
	private void handleBackground(Graphics g) {
		try {
			bg = ImageIO.read(new File("src\\main\\resources\\SusanProjectBG.jpg"));
		}
		catch (IOException e) {
			
		}
		
		g.drawImage(bg, 0, 0, null);
	}
	
	private class MouseHandler extends MouseAdapter {
		public void mousePressed(MouseEvent event) {
			Part part = PartService.getContainingPart(event.getPoint().x, event.getPoint().y);
			if (part != null) {
				StepService.addUserChoiceToBoard(part);
				repaint();
				StateService.handleGameOver();
			}
		}
	}
}
